//
//  PhotoViewController.m
//  UI_随便相册
//
//  Created by dllo on 16/4/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PhotoViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface PhotoViewController ()

@property(nonatomic, retain)UIScrollView *BigScrollView;

@property(nonatomic,retain)UIPageControl *myPageContral;

@property(nonatomic, retain)UIButton *myButton;

@end

@implementation PhotoViewController


-(void)dealloc{
    [_BigScrollView release];
    [_myPageContral release];
    [_myButton release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    
//    NSLog(@"%ld",self.tagNum);
    self.BigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.BigScrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.BigScrollView];
    [_BigScrollView release];
    
    self.BigScrollView.contentSize = CGSizeMake(WIDTH * 9, 0);
    self.BigScrollView.pagingEnabled = YES;
    self.BigScrollView.showsHorizontalScrollIndicator = NO;
    self.BigScrollView.bounces = NO;
    //第一个显示的照片
//klpoophdq
    
    
    
    
    
    self.BigScrollView.contentOffset = CGPointMake(WIDTH, 0);
    //签协议,获取滚动方法
    self.BigScrollView.delegate =self;
    
    
    smallScrollView *isScrollViewFirst = [[smallScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.BigScrollView addSubview:isScrollViewFirst];
    [isScrollViewFirst release];
    
    // 点击哪张图片 进去 就显示哪张图片
    isScrollViewFirst.myImageView.image = [UIImage imageNamed:@"图1.jpg"];
    //签协议,获取缩放方法
    isScrollViewFirst.delegate = self;
    isScrollViewFirst.minimumZoomScale = 0.5;
    isScrollViewFirst.maximumZoomScale = 2.0;
    
    
    
    for (int i = 1; i < 8; i++) {
        smallScrollView *isScrollViewX = [[smallScrollView alloc]initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        [self.BigScrollView addSubview:isScrollViewX];
        [isScrollViewX release];
        
        isScrollViewX.myImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"图%d.jpg",i]];
        //签协议,获取缩放方法
        isScrollViewX.delegate = self;
        isScrollViewX.minimumZoomScale = 0.5;
        isScrollViewX.maximumZoomScale = 2.0;
    }
    
    
    smallScrollView *isScrollViewLast = [[smallScrollView alloc]initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT)];
    [self.BigScrollView addSubview:isScrollViewLast];
    [isScrollViewLast release];
    
    isScrollViewLast.myImageView.image = [UIImage imageNamed:@"图1.jpg"];
    
    //签协议获取缩放方法
    isScrollViewFirst.delegate = self;
    isScrollViewFirst.minimumZoomScale = 0.5;
    isScrollViewFirst.maximumZoomScale = 2.0;
    
    
    self.myPageContral = [[UIPageControl alloc]initWithFrame:CGRectMake(50, HEIGHT - 45, WIDTH - 100, 40)];
    [self.view addSubview:self.myPageContral];
    self.myPageContral.backgroundColor = [UIColor blackColor];
    [_myPageContral release];
    
    self.myPageContral.alpha = 0.3;
    self.myPageContral.layer.masksToBounds = YES;
    self.myPageContral.layer.cornerRadius = 10;
//    self.myPageContral.currentPage = 0;
    self.myPageContral.numberOfPages = 7;
    
    [self.myPageContral addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
    //用到self.tagNum
    //跳转到photo界面,通过self.tagNum调整显示的第一张图片与page的页码数
    self.BigScrollView.contentOffset = CGPointMake(WIDTH * (self.tagNum - 100000 + 1), 0);
    self.myPageContral.currentPage = self.tagNum - 100000;
    
    
    
    //放回按钮
    self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.myButton.frame = CGRectMake(10, 15, 50, 30);
    [self.view addSubview:self.myButton];
    
    [self.myButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.myButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.myButton.layer.masksToBounds = YES;
    self.myButton.layer.cornerRadius = 10;
    self.myButton.layer.borderWidth = 2;
    self.myButton.layer.borderColor = [[UIColor grayColor] CGColor];
    self.myButton.alpha = 0.3;
    
    [self.myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    // Do any additional setup after loading the view.
}

//滚动结束
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    //滚动结束,根据当前照片的偏移量确定page的页码数
    if (scrollView.contentOffset.x / WIDTH > 0 && scrollView.contentOffset.x / WIDTH < 8) {
        self.myPageContral.currentPage = (scrollView.contentOffset.x / WIDTH) - 1;
    }
    if (scrollView.contentOffset.x / WIDTH == 0) {
        self.myPageContral.currentPage = 6;
    }
    
    if (scrollView.contentOffset.x / WIDTH == 8) {
        self.myPageContral.currentPage = 0;
    }
    
    //无限循环
    if (scrollView.contentOffset.x / WIDTH == 0) {
        self.BigScrollView.contentOffset = CGPointMake(WIDTH * 7, 0);
    }
    
    if (scrollView.contentOffset.x / WIDTH == 8) {
        self.BigScrollView.contentOffset = CGPointMake(WIDTH, 0);
    }

    //滑动结束后各个子Scroll的缩放程度都为1.0
    for (UIScrollView *small in scrollView.subviews) {
        if ([small isKindOfClass:[UIScrollView class]]) {
            small.zoomScale = 1.0;
            //滑动结束后各个子Scroll中的照片尺寸恢复满屏大小
            UIImageView *isImageView = [small.subviews firstObject];
            isImageView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        }
    }
    
}


//缩放
//1.允许缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return [scrollView.subviews firstObject];
}

//2.缩放过程中
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    //缩放过程中让照片的中心点位于屏幕的中间,避免跳屏
    UIImageView *isImageView = [scrollView.subviews firstObject];
    isImageView.center = CGPointMake(scrollView.frame.size.width / 2, scrollView.frame.size.height / 2);
    
}

//3.缩放结束
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    //缩放结束让照片的中心点位于屏幕的中间
    UIImageView *isImageView = [scrollView.subviews firstObject];
    isImageView.center = CGPointMake(scrollView.frame.size.width / 2, scrollView.frame.size.height/ 2);
}


//UIPageControl的点击方法
-(void)pageAction:(UIPageControl *)isPage{
    
    //根据page的页数调整照片,一一对应
    //用动画,避免闪跳
    [self.BigScrollView setContentOffset:CGPointMake(WIDTH * (isPage.currentPage + 1), 0) animated:YES];
    
    //让缩放后的图片,滚动回来时还是占满屏
    for (UIScrollView *small in self.BigScrollView.subviews) {
        if ([small isKindOfClass:[UIScrollView class]]) {
            small.zoomScale = 1.0;
            UIImageView *isImageView = [small.subviews firstObject];
            isImageView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        }
    }
    
}


//button的点击方法,返回主界面
-(void)buttonAction:(UIButton *)isBUtton{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
